﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm thực thi script khi tấn công
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yugi.Client.Card;
using ManagementEffect;
using ServerYuGi.PythonRegister;
using ServerYuGi.ChainEffectManager;

namespace ServerYuGi
{
    public partial class SSupervisor
    {


        #region Check_BF_TANCONG
        //public bool Check_BF_TANCONG(int IDPlayerCheck,int IDDefense, int IDAttack, YMonsterCard CardTanCong,int IDPlayerTanCong)
        //{
        //    //----------- Kiểm tra đăng kí lại -----------------//
        //    pyregister.ReCheckDangKyScript(YCheckPoint.MONSTER_ATTACK);
        //    //--------------------------------------------------//



        //    //--------Thuc Hien Check Active-------//
        //    List<string> names = new List<string> { "api_manager", "bi_tan_cong_id", "tan_cong_id", "diem_tan_cong" };
        //    engine.SetVariable(names.ToArray(), api_manager, IDDefense, IDAttack, CardTanCong.Damage);
        //    EffectTurnManager.Check(YCheckPoint.MONSTER_ATTACK, engine);
        //    //------------------------------------------------------------//


        //    //--------------------Kiểm tra điều kiện thõa với list chain hiện có
        //    List<ItemChain> Listchain = ChainMnager.GetListChainNow();
        //    foreach(ItemChain i in Listchain)
        //    {
        //        if (ExEffectManager.ListID_TrongLuot.Contains((byte)i.ID))                
        //            ExEffectManager.ListID_TrongLuot.Remove((byte)i.ID);
        //    }

        //    for (int i = 0; i < ExEffectManager.ListID_TrongLuot.Count;i++ )
        //    {
        //        int speed = LayPlayer(ExEffectManager.IDPlayerCheck).LayCardTrenBan(ExEffectManager.ListID_TrongLuot[i]).MyYBaseCard.Speed;
        //        if(!ChainMnager.CheckCanAddChain(speed))
        //        {
        //            ExEffectManager.ListID_TrongLuot.RemoveAt(i);
        //            i--;
        //        }
        //    }
        //    //-----------------------------------------------------------------//


        //    //----Truong hop ma Player dang check co list excute rong-----//
        //    if (ExEffectManager.ListID_TrongLuot.Count == 0 )
        //    {
        //        Console.WriteLine("--Ko co la nao co the Active--");
        //        ExEffectManager.CountNoCardActive++;
        //        if (ExEffectManager.CountNoCardActive < 2)  // Truong hop ma moi chi 1 player ko co la active, tien hanh kiem tra player con lai
        //        {
        //            SPlayer playerWillcheck = LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck);
        //            ExEffectManager.IDPlayerCheck = playerWillcheck.ID;             
        //            Check_BF_TANCONG(ExEffectManager.IDPlayerCheck, IDDefense, IDAttack, CardTanCong, IDPlayerTanCong);
        //        }
        //        if(ExEffectManager.CountNoCardActive == 2)  //  Truong hop ma ca 2 player deu ko co la nao co the active
        //        {
        //            ChainMnager.ExcuteChain(YCheckPoint.MONSTER_ATTACK);
        //            ChainMnager.Init();
        //            ExEffectManager.InitCount();
        //            ExEffectManager.CountNoCardActive = 0;
        //        }
        //        return ExEffectManager.isResumeProgress;
        //    }       
        //    //--------------------------------------------------------//



        //    //-----------------Truong hop list excute khac rong ---------------------//
        //    List<byte> tmp = new List<byte>();
        //    tmp.Add((byte)ExEffectManager.ListID_TrongLuot.Count);
        //    tmp.AddRange(ExEffectManager.ListID_TrongLuot);
        //    SendMessage_CARD_LIST_CANEXCUTE_ToID(ExEffectManager.IDPlayerCheck,tmp.ToArray());            
        //    WaitManager.Waiting();
        //    //-----------------------------------------------------------------------//



        //    //----------------Truong hop user cancel khong active -------------------//
        //    if(WaitManager.isCancel)
        //    {
        //        ExEffectManager.CountPlayerCanCel++;
        //        ExEffectManager.Init();
        //        if (ExEffectManager.CountPlayerCanCel < 2) // Truong hop ma moi chi 1 player cancel, tien hanh kiem tra player con lai
        //        {
        //            SPlayer playerWillcheck = LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck);
        //            ExEffectManager.IDPlayerCheck = playerWillcheck.ID;                   
        //            Check_BF_TANCONG(ExEffectManager.IDPlayerCheck, IDDefense, IDAttack, CardTanCong, IDPlayerTanCong);
        //        }
        //        if (ExEffectManager.CountPlayerCanCel == 2) // Truong hop ca 2 player cancel
        //        {
        //            ChainMnager.ExcuteChain(YCheckPoint.MONSTER_ATTACK);
        //            ChainMnager.Init();
        //            ExEffectManager.InitCount();
        //        }
        //        return ExEffectManager.isResumeProgress;
        //    }
        //    //-----------------------------------------------------------------------//


        //    //-----------------------Truong hop user da chon 1 la active---------------//
        //    int IDcardActive = WaitManager.GetResult().First();
        //    int SpeedCardActive = LayPlayer(ExEffectManager.IDPlayerCheck).LayCardTrenBan(IDcardActive).MyYBaseCard.Speed;
        //    ChainMnager.AddChain(YCheckPoint.MONSTER_ATTACK,IDcardActive,SpeedCardActive);
        //    //-------------------------------------------------------------------------//
            

        //    ExEffectManager.InitCount();
        //    ExEffectManager.Init();
        //    SPlayer playerWillCheck = LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck);
        //    ExEffectManager.IDPlayerCheck = playerWillCheck.ID;            
        //    Check_BF_TANCONG(ExEffectManager.IDPlayerCheck, IDDefense, IDAttack, CardTanCong, IDPlayerTanCong);
        //    return ExEffectManager.isResumeProgress;
        //}
        #endregion


       

        public bool Check_EffectExcute(List<YCheckPoint> CheckPoint,int IDPlayerCheck,string name,params object[] param)
        {
           

            //----------- Kiểm tra đăng kí lại -----------------//
            for (int i = 0; i < CheckPoint.Count; i++)
            {
                Console.WriteLine("**************Check CheckPoint = " + CheckPoint[i].ToString());
                pyregister.ReCheckDangKyScript(CheckPoint[i]);
            }
                
            //--------------------------------------------------//

            string[] nameSet = name.Split(",".ToCharArray());
            

            //--------Thuc Hien Check Active-------//
            
            //engine.SetVariable(ExEffectManager.nameAddToScript.ToArray(), param);
            engine.SetVariable(nameSet, param);
            for (int i = 0; i < CheckPoint.Count;i++ )
            {
                EffectTurnManager.CheckNoAsk(CheckPoint[i], engine, this);
                EffectTurnManager.Check(CheckPoint[i], engine, this);
            }
            
            //------------------------------------------------------------//


            //--------------------Kiểm tra điều kiện thõa với list chain hiện có
            List<ItemChain> Listchain = ChainMnager.GetListChainNow();
            foreach (ItemChain i in Listchain)
            {
                if (ExEffectManager.ListID_TrongLuot.Contains((byte)i.ID))
                    ExEffectManager.ListID_TrongLuot.Remove((byte)i.ID);
            }
            SPlayer playercheck = LayPlayer(ExEffectManager.IDPlayerCheck);
            for (int i = 0; i < ExEffectManager.ListID_TrongLuot.Count; i++)
            {
                int speed = playercheck.LayCardInAll(ExEffectManager.ListID_TrongLuot[i]).MyYBaseCard.Speed;
                if (!ChainMnager.CheckCanAddChain(speed))
                {
                    ExEffectManager.ListID_TrongLuot.RemoveAt(i);
                    i--;
                }
            }
            for (int i = 0; i < ExEffectManager.ListID_TrongLuot.Count; i++)
            {
                SCard item = playercheck.LayCardInAll(ExEffectManager.ListID_TrongLuot[i]);
                switch (item.MyYBaseCard.CardType)
                {
                    case YType.Monster:
                        if (!playercheck.Block.DuocPhep_ActiveMonster)
                        {
                            ExEffectManager.ListID_TrongLuot.RemoveAt(i);
                            i--;
                        }
                	    break;
                    case YType.Spell:
                        if (!playercheck.Block.DuocPhep_ActiveSpell)
                        {
                            ExEffectManager.ListID_TrongLuot.RemoveAt(i);
                            i--;
                        }
                        break;
                    case YType.Trap:
                        if (!playercheck.Block.DuocPhep_ActiveTrap)
                        {
                            ExEffectManager.ListID_TrongLuot.RemoveAt(i);
                            i--;
                        }
                        break;
                }
            }

            //-----------------------------------------------------------------//


            //----Truong hop ma Player dang check co list excute rong-----//
            if (ExEffectManager.ListID_TrongLuot.Count == 0)
            {
                Console.WriteLine("--Ko co la nao co the Active--");
                ExEffectManager.CountNoCardActive++;
                if (ExEffectManager.CountNoCardActive < 2)  // Truong hop ma moi chi 1 player ko co la active, tien hanh kiem tra player con lai
                {
                    if (ExEffectManager.CountPlayerCanCel == 1)
                    {
                        ExEffectManager.InitCount();
                        ChainMnager.ExcuteChain();
                        ChainMnager.Init();                 
                        
                        return ExEffectManager.isResumeProgress;
                    }
                    SPlayer playerWillcheck = LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck);
                    ExEffectManager.IDPlayerCheck = playerWillcheck.ID;
                    Check_EffectExcute(CheckPoint, ExEffectManager.IDPlayerCheck,name, param);
                }
                if (ExEffectManager.CountNoCardActive == 2)  //  Truong hop ma ca 2 player deu ko co la nao co the active
                {
                    ExEffectManager.InitCount();
                    ChainMnager.ExcuteChain();
                    ChainMnager.Init();                    
                    
                }
                return ExEffectManager.isResumeProgress;
            }
            //--------------------------------------------------------//



            //-----------------Truong hop list excute khac rong ---------------------//
            List<byte> tmp = new List<byte>();
            tmp.Add((byte)ExEffectManager.ListID_TrongLuot.Count);
            tmp.AddRange(ExEffectManager.ListID_TrongLuot);
            SendMessage_CARD_LIST_CANEXCUTE_ToID(ExEffectManager.IDPlayerCheck, tmp.ToArray());
            
            WaitManager.Waiting();
            //-----------------------------------------------------------------------//
            SendMessage_CARD_WAITING_ACTIVE_END_ToID(LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck).ID);


            //----------------Truong hop user cancel khong active -------------------//
            if (WaitManager.isCancel)
            {
                AddStore_Cancel(ExEffectManager.ListID_TrongLuot); // truong hop loai 2,3. dung de add store khi player cancel

                ExEffectManager.CountPlayerCanCel++;
                ExEffectManager.Init();
                if (ExEffectManager.CountPlayerCanCel < 2) // Truong hop ma moi chi 1 player cancel, tien hanh kiem tra player con lai
                {
                    SPlayer playerWillcheck = LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck);
                    ExEffectManager.IDPlayerCheck = playerWillcheck.ID;
                    Check_EffectExcute(CheckPoint, ExEffectManager.IDPlayerCheck,name, param);
                }
                if (ExEffectManager.CountPlayerCanCel == 2) // Truong hop ca 2 player cancel
                {
                    ExEffectManager.InitCount();
                    ChainMnager.ExcuteChain();
                    ChainMnager.Init();
                    
                }
                return ExEffectManager.isResumeProgress;
            }
            //-----------------------------------------------------------------------//

            ChainMnager.isActiveChain = false;

            //-----------------------Truong hop user da chon 1 la active---------------//
            int IDcardActive = WaitManager.GetResult().First();
            SCard CardAddChain = LayPlayer(ExEffectManager.IDPlayerCheck).LayCardInAll(IDcardActive);
            int SpeedCardActive = CardAddChain.MyYBaseCard.Speed;            
            ChainMnager.AddChain(CheckPoint,IDcardActive,CardAddChain.MyYBaseCard.Card_TypeID ,SpeedCardActive);
            //-------------------------------------------------------------------------//

            // Xoa Store la active neu co
            pyregister.RemoveIDinStore(CardAddChain);


            ExEffectManager.InitCount();
            ExEffectManager.Init();
            SPlayer playerWillCheck = LayPlayerKhacVoiID(ExEffectManager.IDPlayerCheck);
            ExEffectManager.IDPlayerCheck = playerWillCheck.ID;
            
            //------------------------Check Trap,Spell Active ----------------------------------//
            CheckMagic(CardAddChain,CheckPoint,name,param);
            //---------------------------------------------------------------------------//

            if (ChainMnager.isActiveChain)   
            {
                ChainMnager.isActiveChain = false;
                return ExEffectManager.isResumeProgress;
            }
          

            Check_EffectExcute(CheckPoint, ExEffectManager.IDPlayerCheck,name, param);
            return ExEffectManager.isResumeProgress;
        }

        void AddStore_Cancel(List<byte> ListID)
        {
            SPlayer player = LayPlayer(ExEffectManager.IDPlayerCheck);
            foreach (byte i in ListID)
            {
                SCard card = player.LayCardTrenBan(i);
                if (card == null)
                    card = player.LayCardTrenTay(i);
                CardScript script = CScriptDB.GetScript(card.MyYBaseCard.Card_TypeID);
                pyregister.AddStore(script.GetListCheckPoint(), i);
            }
        }

        void CheckMagic(SCard card,List<YCheckPoint> CheckPointS,string name,object[] param)
        {
           // bool check = false;
            List<string> tmp = name.Split(",".ToCharArray()).ToList();
            string type_last_chain = "type_last_chain";
            bool isSee = false;
            for (int i = tmp.Count-1; i >=0;i-- )
            {
                if (tmp.Equals(type_last_chain))
                {
                    isSee = true;
                    break;
                }
            }
            List<object> obj = param.ToList();
            List<YCheckPoint> Listcheckpoint = null;
            Listcheckpoint = CheckPointS;
            

            
            switch (card.MyYBaseCard.CardType)
            {
                case YType.Trap:                    
                    ExEffectManager.CheckPointNow = YCheckPoint.TRAP_ACTIVE;
                    if(!isSee)
                    {
                        tmp.Add(type_last_chain);
                        obj.Add(card.MyYBaseCard.CardType);
                    }
                    if (!Listcheckpoint.Contains(YCheckPoint.TRAP_ACTIVE))
                        Listcheckpoint.Add(YCheckPoint.TRAP_ACTIVE);
                    string StringTrap = String.Join(",", tmp.ToArray());
                    Check_EffectExcute(Listcheckpoint, ExEffectManager.IDPlayerCheck, StringTrap,obj.ToArray());
                    
                    break;
                case YType.Spell:
                    ExEffectManager.CheckPointNow = YCheckPoint.SPELL_ACTIVE;
                    if (!isSee)
                    {
                        tmp.Add(type_last_chain);
                        obj.Add(card.MyYBaseCard.CardType);
                    }
                    if (!Listcheckpoint.Contains(YCheckPoint.SPELL_ACTIVE))
                        Listcheckpoint.Add(YCheckPoint.SPELL_ACTIVE);
                    string StringSet = String.Join(",", tmp.ToArray());
                    Check_EffectExcute(Listcheckpoint, ExEffectManager.IDPlayerCheck, StringSet, obj.ToArray());
                    break;
            }
            //return check;
        }


        void CheckActiveNoQuestion()
        {

        }
            
    }
}
